import {
  Controller,
  Post,
  UseInterceptors,
  UploadedFile,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { MinioService } from '../minio/minio.service';

@Controller('files')
export class FileController {
  constructor(private readonly minioService: MinioService) {}

  @Post('upload')
  @UseInterceptors(FileInterceptor('file')) // 使用 Multer 的 FileInterceptor 处理文件上传
  async uploadFile(@UploadedFile() file: Express.Multer.File) {
    const bucketName = 'zfulin'; // MinIO 桶的名称
    try {
      const etag = await this.minioService.uploadFile(bucketName, file);
      return { message: 'File uploaded successfully', etag };
    } catch (error) {
      throw new Error('Failed to upload file: ' + error.message);
    }
  }
}
